package com.yuuzy.view;

import com.yuuzy.dao.ClientDAO;
import com.yuuzy.model.Client;
import com.yuuzy.utils.StringUtils;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class insertClientInterFrm extends JInternalFrame {
    private JTextField client_nameTxt;
    private JTextField client_telephoneTxt;
    private JTextField client_idnumberTxt;
    private JTextField client_emailTxt;

    private ClientDAO clientDAO = new ClientDAO();
    private JTextField client_passwordTxt;
    private JTextField client_balanceTxt;

    /**
     * Create the frame.
     */
    public insertClientInterFrm() {
        setClosable(true);
        setIconifiable(true);
        setTitle("添加用户");
        setBounds(100, 100, 598, 430);

        JLabel lblNewLabel_1 = new JLabel("客户姓名：");

        JLabel lblNewLabel_2 = new JLabel("");

        JLabel lblNewLabel_3 = new JLabel("客户电话：");

        JLabel lblNewLabel_4 = new JLabel("客户身份证：");

        JLabel lblNewLabel_5 = new JLabel("客户邮箱：");

        JLabel lblNewLabel_6 = new JLabel("用户密码：");

        client_nameTxt = new JTextField();
        client_nameTxt.setColumns(10);

        client_telephoneTxt = new JTextField();
        client_telephoneTxt.setColumns(10);

        client_idnumberTxt = new JTextField();
        client_idnumberTxt.setColumns(10);

        client_emailTxt = new JTextField();
        client_emailTxt.setColumns(10);

        JButton btnNewButton = new JButton("添加");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                insertClientActionPerformed(e);
            }
        });

        JButton button = new JButton("重置");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                resetValueActionPerformed(e);
            }
        });

        client_passwordTxt = new JTextField();
        client_passwordTxt.setColumns(10);

        JLabel lblNewLabel = new JLabel("用户余额：");

        client_balanceTxt = new JTextField();
        client_balanceTxt.setColumns(10);
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(btnNewButton))
                                        .addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
                                                .addGap(109)
                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                                        .addComponent(lblNewLabel_4)
                                                        .addComponent(lblNewLabel_1)
                                                        .addComponent(lblNewLabel_3)
                                                        .addComponent(lblNewLabel_2)
                                                        .addComponent(lblNewLabel_5)
                                                        .addComponent(lblNewLabel_6)
                                                        .addComponent(lblNewLabel))))
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                                        .addComponent(client_nameTxt, GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
                                                        .addComponent(client_telephoneTxt, GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
                                                        .addComponent(client_idnumberTxt, GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
                                                        .addComponent(client_passwordTxt, GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
                                                        .addComponent(client_emailTxt, GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
                                                        .addComponent(client_balanceTxt, GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE))
                                                .addGap(176))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(66)
                                                .addComponent(button)
                                                .addContainerGap())))
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGap(58)
                                .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addComponent(lblNewLabel_1)
                                                .addGap(19))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addComponent(client_nameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                .addGap(18)))
                                .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
                                        .addComponent(lblNewLabel_2)
                                        .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                                .addComponent(lblNewLabel_3)
                                                .addComponent(client_telephoneTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                                .addGap(18)
                                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_4)
                                        .addComponent(client_idnumberTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(18)
                                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_5)
                                        .addComponent(client_emailTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(18)
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addComponent(client_passwordTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(lblNewLabel_6))
                                .addGap(18)
                                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel)
                                        .addComponent(client_balanceTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(btnNewButton)
                                        .addComponent(button))
                                .addGap(32))
        );
        getContentPane().setLayout(groupLayout);


    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    insertClientInterFrm frame = new insertClientInterFrm();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    //客户类别添加事件处理
    private void insertClientActionPerformed(ActionEvent evt) {
        String client_name = this.client_nameTxt.getText();
        String client_telephone = this.client_telephoneTxt.getText();
        String client_idnumber = this.client_idnumberTxt.getText();
        String client_email = this.client_emailTxt.getText();
        String client_password = this.client_passwordTxt.getText();
        String client_balance = this.client_balanceTxt.getText();

        if (StringUtils.isEmpty(client_name)) {
            JOptionPane.showMessageDialog(null, "用户姓名不能为空!");
            return;
        }
        if (StringUtils.isEmpty(client_telephone)) {
            JOptionPane.showMessageDialog(null, "用户电话不能为空!");
            return;
        }
        if (StringUtils.isEmpty(client_idnumber)) {
            JOptionPane.showMessageDialog(null, "用户身份证不能为空!");
            return;
        }
        if (StringUtils.isEmpty(client_email)) {
            JOptionPane.showMessageDialog(null, "用户邮箱不能为空!");
            return;
        }
        if (StringUtils.isEmpty(client_password)) {
            JOptionPane.showMessageDialog(null, "用户密码不能为空!");
            return;
        }
        if (StringUtils.isEmpty(client_balance)) {
            JOptionPane.showMessageDialog(null, "用户余额不能为空!");
            return;
        }

        Client client = new Client(client_name, client_telephone, client_idnumber, client_email, client_password, StringUtils.changeDouble(client_balance));


        int n = clientDAO.insertClient(client);

        if (n > 0) {
            JOptionPane.showMessageDialog(null, "用户添加成功!");
            resetValue();
        } else {
            JOptionPane.showMessageDialog(null, "用户添加失败!");
        }


    }

    //重置事件处理
    private void resetValueActionPerformed(ActionEvent evt) {
        this.resetValue();

    }

    /**
     * 重置表单
     */
    private void resetValue() {
        this.client_nameTxt.setText("");
        this.client_telephoneTxt.setText("");
        this.client_idnumberTxt.setText("");
        this.client_emailTxt.setText("");
        this.client_passwordTxt.setText("");
        this.client_balanceTxt.setText("");

    }
}

